/**
 * 应用的核心js文件
 */
/*@ start to scor*/
grApp.controller('CenterCtrl',['$scope','$http','BaseService','evaluationFactory',function($scope,$http,BaseService,evaluationFactory){

    $scope.wx_inited = false;
    if (typeof WeixinJSBridge == "undefined"){
        if( document.addEventListener ){
            document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
        }else if (document.attachEvent){
            document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
        }
    }else{
        onBridgeReady();
    }
    function onBridgeReady(){
        $scope.wx_inited = true;
        //alert('ok');
        //todo some wx things
    }

    $scope.jump_scor=function(args){
        location.href=(args);
    };
    $scope.jump_to_list_scor=function(args){
        location.href=('center/history');
    };
    var height = $(".height-width").width();
    $(".height-width").css("height",height );
    $(".height-width p").css("padding-top",height/4+"px" );
    $(".height-width-child").css("height",height );
    $(".height-width-child p").css("line-height",height+"px");

    $scope.getOrderByType=function(args){
        $http({
            method: 'GET',
            url: args,
        }).success(function (data, status, headers, config){
            $scope.orderlistres = data.listtype;
        }).error(function (data, status, headers, config){
            alert("查询异常！");
        });
    };

    $scope.load=function(ol){
        $scope.isqb='';
        $scope.isdfk='';
        $scope.isdsh='';
        $scope.isdpj='';

        switch (ol) {
            case '/user/nghistory/0':
                $scope.isqb='am-active';
                break;
            case '/user/nghistory/1':
                $scope.isdfk='am-active';
                break;
            case '/user/nghistory/3':
                $scope.isdsh='am-active';
                break;
            case '/user/nghistory/4':
                $scope.isdpj='am-active';
                break;
        }

        $http({
            method: 'GET',
            url: ol,
        }).success(function (data, status, headers, config){
            $scope.orderlistres = data.listtype;
        }).error(function (data, status, headers, config){
            alert("查询异常！");
        });
    };

    $scope.pinjia=function(args){
        //alert(args);
        location.href=('/user/eva/'+args);
    };

    $scope.getOrderInfo=function(args){
        alert('a');
        //location.href=(""+args);
    };

    //$scope.getUID=function(uid){
    //    $scope.yeid=uid;
    //}

    $scope.rechargeAmount = 100;//默认充值金额100元

    $scope.isActive = {i:true};
    $scope.chooseRecharge = function(amount,i){
        $scope.isActive = {};
        $scope.isActive[i] = true;
        $scope.rechargeAmount = amount;
    };

    $scope.doRecharge = function(){
        var timestamp = new Date().getTime();
        var orderid = 'C'+timestamp;

        //var uid = $scope.yeid;
        //var fee = parseFloat(args)*100;
        $http({
            methd:'GET',
            url:'/user/recharge/'+orderid+'/'+$scope.rechargeAmount,
        }).success(function(datas,status,headers,config){
            if(datas.code!=0){
                alert(">_< ~ OMG! 果小然内部发生了错误，待会再来试试看！");
                return;
            }
            //alert(datas.baocun);
            var fee = parseFloat($scope.rechargeAmount);//正式环境中要乘100
            fee=fee*100;
            if($scope.wx_inited){
                BaseService.wxpay(orderid,fee).then(function(data){
                    //更新状态为1500
                    //todo...
                    alert("噢耶~充值成功!快去个人中心看看吧~");
                    window.location.href='/user/center';
                }).catch(function(err){
                    alert(">_< ~ OMG! 就这么残忍的拒绝我了么?");
                });
            }else{
                alert('网络链接速度过慢，微信支付未能初始化成功~\n请刷新重试一下');
                window.location.href='/user/center';
            }
        }).error(function(data, status, headers, config){
            alert(">_< ~ OMG! 果小然内部发生了错误，待会再来试试看！");
        });

    };

    $scope.chongZhi=function(args){

        var timestamp = new Date().getTime();
        var orderid = 'C'+timestamp;

        //var uid = $scope.yeid;

        //var fee = parseFloat(args)*100;
        $http({
            methd:'GET',
            url:'/user/recharge/'+orderid+'/'+args,
        }).success(function(datas,status,headers,config){
            //alert(datas.baocun);
            var fee = parseFloat(args);//正式环境中要乘100
            fee=fee*100;
            if($scope.wx_inited){
                BaseService.wxpay(orderid,fee).then(function(data){
                    alert("支付成功");
                    window.location.href='/user/center';
                }).catch(function(err){
                    alert("支付失败");
                });
            }else{
                alert('网络链接速度过慢，微信支付未能初始化成功~\n请刷新重试一下');
                window.location.href='/user/center';
            }
        }).error(function(data, status, headers, config){
            alert("生成余额充值订单异常！");
        });

    };

    //判断用户是否绑定手机
    $scope.ifBingPhone=function(args){
        $http({
            methd:'GET',
            url:'/user/ifbindphone',
        }).success(function(data,status,headers,config){
            if(!data.bindphone) {
                //没有绑定手机，弹出模态框，进行绑定
                var value='';
                if('c'==args){
                    value = prompt("亲～绑定一下手机号，可以吗？");
                    //alert(value);
                }else if('m'==args){
                    value = prompt("请输入您要绑定的手机号：");
                }
                var re = /^1\d{10}$/;
                if(value !==null) {
                    if (re.test(value)) {
                        $scope.bindPhone(value);
                    } else {
                        alert('手机号码格式不对哦！');
                    }
                }
            }else{
                if('m'==args) {
                    alert('亲，您已经绑定过手机号啦！');
                }
            }
        }).error(function(data,status,headers,config){
            alert('网络连接异常！');
        });
    };
    //绑定手机
     $scope.bindPhone=function(args){
       $http({
           methd:'GET',
           url:'/user/bindphone/'+args,
       }).success(function(data, status, headers, config){
           alert('手机绑定成功，谢谢！');
       }).error(function(data, status, headers, config){
          alert('网络连接异常！');
       });
     };

    //去付款
    $scope.gotopay=function(args){
        //alert($scope.payid+args);
        if(args=='wx_pay'){
            var fee = parseFloat($scope.payfee);//正式环境中要乘100
            fee=fee*100;
            if($scope.wx_inited){
                BaseService.wxpay($scope.payid,fee).then(function(data){
                    alert("支付成功");
                    window.location.href='/order/confirmOrder/again';
                }).catch(function(err){
                    alert("支付失败");
                });
            }else{
                alert('网络链接速度过慢，微信支付未能初始化成功~\n请刷新重试一下');
                window.location.href='/user/center';
            }
        }else{
            $http({
                methd:'GET',
                url:'/order/againPay',
                params:{orderid:$scope.payid,payway:args},
            }).success(function(data, status, headers, config){
                if(data.code == '9990'){
                    alert("该订单已付款，无法付款哦！");
                }else if(data.code == '9991'){
                    alert("余额不足，请选择其它支付方式哦！");
                }else if(data.code == '9999'){
                    alert("系统出现问题，支付失败，如有疑问，请联系客服");
                }else{
                    alert("恭喜您，下单成功！");
                    window.location.href='/order/confirmOrder/again';
                }

            }).error(function(data, status, headers, config){
                alert("下单失败！");
                window.location.href='/order/confirmOrder/again';
            });
        }

    };

    //取消订单
    $scope.quxiao=function(args){
        //alert(args);
        //location.href=('/order/setOrderCancel/'+args);
        var ecconfirm = window.confirm("撤单提醒：如果使用了优惠卷，该笔订单所用优惠卷将作废。您确定要撤销这笔未付款订单吗？");
        if (!ecconfirm) {
            return;
        }


        $http({
            methd:'GET',
            url:'/order/setOrderCancel/'+args,
            //params:{objectId:args},
        }).success(function(data, status, headers, config){
            alert("订单取消成功！");
            window.location.href='/order/confirmOrder/again';
        }).error(function(data, status, headers, config){
            alert("订单取消失败！");
            window.location.href='/order/confirmOrder/again';
        });

    };
    /*
    * 订单确认收货
    * */
    $scope.shouhuo=function(args){
        //alert(args);
        $http({
            methd:'GET',
            url:'/user/shouhuo/'+args,
            //params:{orderid:args},
        }).success(function(data, status, headers, config){
            alert("确认收货成功！");
            window.location.href='/user/history/3';
        }).error(function(data, status, headers, config){
            alert("确认收货失败！");
            window.location.href='/user/history/3';
        });
    };

    $scope.show = function(orderid,payfee){
        //alert(orderid);
        //alert(orderid);
        //alert(payfee);
        $scope.payid=orderid;
        $scope.payfee=payfee;
        $('#my-actions').modal();
    };

    $scope.chuli=function(args){
        args = JSON.parse(args);
        //alert(args[0].orderid);
        $scope.orderid=args[0].orderid;
        $scope.evagoods=args[0].goods;
        $scope.iconstart1='am-icon-star';
        $scope.iconstart2='am-icon-star';
        $scope.iconstart3='am-icon-star';
        $scope.iconstart4='am-icon-star';
        $scope.iconstart5='am-icon-star';
        $scope.starfor='好评';
    };

    $scope.czkcz=function(){
        //alert(1);
        //var ecconfirm = window.confirm("尊敬的果粉，请您确认充值卡卡密是正确的。如果您确认完毕，请点击‘确定’按钮继续操作。");
        //if (!ecconfirm) {
        //    return;
        //}

        document.getElementById("cz").disabled=true;

        var km = $scope.km;

        if(km == null || km == ''){
            alert('充值卡密钥不能为空，请输入充值卡密钥。');
            document.getElementById("cz").disabled=false;
            return false;
        //}
        }

        BaseService.czkcz(km).then(function(data){
            if(data.code == 0){
                alert("充值成功");
                window.location.href='/user/center';
            }else{
                if(data.code == 511){
                    alert("卡密钥未激活，请联系客服");
                    document.getElementById("cz").disabled=false;
                }else{
                    alert("充值失败");
                    window.location.href='/user/center';
                }
            }
        }).catch(function(err){
            alert("充值失败");
        });
    };

    $scope.clickstar=function(args){
        //alert(args);
        switch (args.charAt(0)) {
            case '1':
                //$scope.iconstart1='am-icon-star';
                //$scope.iconstart2='am-icon-star-o';
                //$scope.iconstart3='am-icon-star-o';
                //$scope.iconstart4='am-icon-star-o';
                //$scope.iconstart5='am-icon-star-o';
                $('#'+args.substr(1)+'s1').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s2').removeClass().addClass('am-icon-star-o');
                $('#'+args.substr(1)+'s3').removeClass().addClass('am-icon-star-o');
                $('#'+args.substr(1)+'s4').removeClass().addClass('am-icon-star-o');
                $('#'+args.substr(1)+'s5').removeClass().addClass('am-icon-star-o');

                $('#'+args.substr(1)+'star').text('1星差评');
                //alert('#'+args.substr(1)+'star');
                break;
            case '2':
                //$scope.iconstart1='am-icon-star';
                //$scope.iconstart2='am-icon-star';
                //$scope.iconstart3='am-icon-star-o';
                //$scope.iconstart4='am-icon-star-o';
                //$scope.iconstart5='am-icon-star-o';

                $('#'+args.substr(1)+'s1').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s2').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s3').removeClass().addClass('am-icon-star-o');
                $('#'+args.substr(1)+'s4').removeClass().addClass('am-icon-star-o');
                $('#'+args.substr(1)+'s5').removeClass().addClass('am-icon-star-o');

                $('#'+args.substr(1)+'star').text('2星中评');
                break;
            case '3':
                //$scope.iconstart1='am-icon-star';
                //$scope.iconstart2='am-icon-star';
                //$scope.iconstart3='am-icon-star';
                //$scope.iconstart4='am-icon-star-o';
                //$scope.iconstart5='am-icon-star-o';

                $('#'+args.substr(1)+'s1').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s2').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s3').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s4').removeClass().addClass('am-icon-star-o');
                $('#'+args.substr(1)+'s5').removeClass().addClass('am-icon-star-o');

                $('#'+args.substr(1)+'star').text('3星中评');
                break;
            case '4':
                //$scope.iconstart1='am-icon-star';
                //$scope.iconstart2='am-icon-star';
                //$scope.iconstart3='am-icon-star';
                //$scope.iconstart4='am-icon-star';
                //$scope.iconstart5='am-icon-star-o';

                $('#'+args.substr(1)+'s1').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s2').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s3').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s4').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s5').removeClass().addClass('am-icon-star-o');

                $('#'+args.substr(1)+'star').text('4星好评');
                break;
            case '5':
                //$scope.iconstart1='am-icon-star';
                //$scope.iconstart2='am-icon-star';
                //$scope.iconstart3='am-icon-star';
                //$scope.iconstart4='am-icon-star';
                //$scope.iconstart5='am-icon-star';

                $('#'+args.substr(1)+'s1').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s2').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s3').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s4').removeClass().addClass('am-icon-star');
                $('#'+args.substr(1)+'s5').removeClass().addClass('am-icon-star');

                $('#'+args.substr(1)+'star').text('5星好评');
                break;
        }
    };

    $scope.sendeva=function(args){
        //alert($scope.evagoods);
        //goToSaveEva
        var goodsPj = {} ;
        var goods = [];
        var orderid = $scope.orderid ;
        goodsPj['orderid'] = orderid;
        async.eachSeries($scope.evagoods,function(eachGood,callback){
            var goodsPjcontent = {} ;
            var wenzi=$('#'+eachGood.pid+'').val();//评价内容
            var pjia=$('#'+eachGood.pid+'star').text() || '5';//星级
            var pid = eachGood.pid ;
            var pname = eachGood.pname ;
            goodsPjcontent['pjia'] = pjia;
            goodsPjcontent['wenzi'] = wenzi;
            goodsPjcontent['pid'] = pid;
            goodsPjcontent['pname'] = pname;
            goods.push(goodsPjcontent);
            callback(null, goods);
            /*$http({
                methd:'GET',
                url:'/user/saveeva/'+$scope.orderid+'/'+wenzi+'/'+eachGood.pid+'/'+eachGood.pname+'/'+pjia.charAt(0),
            }).success(function(data, status, headers, config){
                callback(null);
            }).error(function(data, status, headers, config){
                //alert("下单失败！");
                callback(null);
            });*/
        },function(){
            goodsPj['content'] = goods ;
            evaluationFactory.goToSaveEva(goodsPj).then(function(data){
                if(data){
                    alert('订单评价成功！');
                    window.location.href='/user/history/4'; //跳转到全部订单
                }else{
                    alert('订单评价失败！');
                }
            }).catch(function(err){
                alert('订单评价失败了！');
            });

        });
    };

    //判断用户是否已经设置支付密码
    $scope.ifsetpwd=function(args){
        $http({
            methd:'GET',
            url:'/user/ifsetpwd'
        }).success(function(data, status, headers, config){
            if(data.ifset){
                window.location.href='/user/password/xg';
            }else{
                window.location.href='/user/password/sz';
            }
        }).error(function(data, status, headers, config){

        });

    };
}]);
/*@ end to scor*/

grApp.factory('evaluationFactory',['$http','$q',function($http,$q){
    var service = {};
    //订单评价
    service.goToSaveEva = function(goods){
        var deferred = $q.defer(); // 声明延后执行，表示要去监控后面的执行
        $http({
            methd:'GET',
            url:'/user/newsaveeva',
            params:{goods:goods}
        }).success(function(data, status, headers, config) {
            deferred.resolve(data);
        }).error(function(data, status, headers, config) {
            deferred.reject(data);
        });
        return deferred.promise;
    };
    return service;
}]);

